package com.sky.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sky.blog.dao.mapper.BlogTopicMapper;
import com.sky.blog.dao.pojo.BlogTopic;
import com.sky.blog.dao.vo.BlogTopicsVo;
import com.sky.blog.service.BlogTopicService;
import com.sky.blog.util.DozerUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author sky  2022/3/28
 * @version 1.0
 */
@Slf4j
@Service
public class BlogTopicServiceImpl implements BlogTopicService {

    private final static String URI_SEPARATOR = "/";

    @Resource
    private BlogTopicMapper blogTopicMapper;

    @Override
    public List<BlogTopicsVo> getManyBlogTopics(int many) {
        if (many < 0) {
            many = 3;
        }
        LambdaQueryWrapper<BlogTopic> wrapper = Wrappers.lambdaQuery(BlogTopic.class);
        wrapper.orderByAsc(BlogTopic::getSort)
                .orderByDesc(BlogTopic::getGmtCreated);
        wrapper.last("limit 0," + many);
        return blogTopicMapper.selectList(wrapper)
                .stream().map(topics -> DozerUtils.copy(topics, BlogTopicsVo.class))
                .collect(Collectors.toList());
    }
}
